import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.13
import QtQuick.Dialogs 1.0
//import UIClientApi 1.0
//import UIContacts 1.0
import ListControl 1.0
ApplicationWindow{
    id :mainWindows
    x:Screen.desktopAvailableWidth/2-width/2
    y: Screen.desktopAvailableHeight/2-height/2
    width: Screen.desktopAvailableWidth*0.5
    height: Screen.desktopAvailableHeight*0.7
    title: qsTr("TaIM App")

    // API
    ListControl{
        id:listApi
    }

    SystemIcon {
        id:systemIcon
    }
    //页面选择
    property bool chooesMessage: true
    property bool chooesAddress: false
    property bool chooesSetting: false
    property bool haveSession: false
    property int chooesIconSize: 24 // 工具栏 图标大小
    // 当前选择的联系人 id 和 名称
    property string sessionName: "C++Wu"
    property string sessionID: "111"

    property bool openFileWindow: false


    function doChooesMessage(){
        swipeView.currentIndex=0
        chooesMessage=true
        chooesAddress=false
        chooesSetting=false
        return true
    }

    function doChooesAddress(){
        swipeView.currentIndex=1
        chooesMessage=false
        chooesAddress=true
        chooesSetting=false
        return true
    }

    function doChooesSetting(){
        swipeView.currentIndex=2
        chooesMessage=false
        chooesAddress=false
        chooesSetting=true
        return true
    }
    function getStatus(){
        return "#27f355"
    }

    FileDialog {
        id: fileDialog
        title: "Please choose a file"
        folder: shortcuts.home
        onAccepted: {
            console.log("You chose: " + fileDialog.fileUrls)
            // 上传文件的动作
            if(fileDialog.fileUrls[0].substring(0,7)=="file://"){
                console.log("is file")
                var filepath=fileDialog.fileUrls[0].substring(7)
                // 获取文件名
                var spliePath=filepath.split("/");
                var fileName=spliePath[spliePath.length-1]
                console.log("filename ",fileName)
                // 测试
                listApi.sendMessageByFile("123",1,filepath,fileName)
            }

            console.log("file ",filepath)
            visible=false
        }
        onRejected: {
            console.log("Canceled")
            visible=false
        }
        Component.onCompleted: visible = false
    }
    // 头部工具栏
    header:Rectangle{
        id:mainWindows_header
        height: 64
        color: "#e1e1e1"
        border.width: 1
        border.color: "#b4b4b4"

        width: parent.width
        antialiasing: false

        // tool
        // mian ui choose
        Image {
            x:parent.width/2 - chooesIconSize*2
            y:20
            width: chooesIconSize
            height: chooesIconSize
            source:chooesMessage?"qrc:/other/icon/message_ed.png":"qrc:/other/icon/message.png"
            Rectangle{
                MouseArea{
                    width: chooesIconSize
                    height: chooesIconSize
                    onClicked:{
                        doChooesMessage()
                        console.log("aaa")
                    }
                }
            }
        }


        Image {
            x:parent.width/2
            y:20
            width: chooesIconSize
            height: chooesIconSize
            source: chooesAddress?"qrc:/other/icon/address_list_ed.png":"qrc:/other/icon/address_list.png"
            Rectangle{
                MouseArea{
                    width: chooesIconSize
                    height: chooesIconSize
                    onClicked: {
                        doChooesAddress()
                        console.log("adress")
                    }
                }
            }
        }

        Image {
            x:parent.width/2+ chooesIconSize*2
            y:20
            width: chooesIconSize
            height: chooesIconSize
            source:chooesSetting?"qrc:/other/icon/settings_ed.png":"qrc:/other/icon/settings.png"
            Rectangle{
                MouseArea{
                    width: chooesIconSize
                    height: chooesIconSize
                    onClicked: {
                        doChooesSetting()
                        console.log("setting")
                    }
                }
            }
        }

        Rectangle{
            id: findAccount
            x: 26
            y: 22
            width: 128
            height: 24
            radius: 9
            TextInput {
                id: textInput
                width: 80
                height: 24
                text: qsTr("user/team")
                anchors.verticalCenterOffset: 2
                anchors.verticalCenter: parent.verticalCenter
                leftPadding: 4
                font.pixelSize: 12
            }
        }
        Rectangle{
            x:findAccount.width+findAccount.x-24
            y:findAccount.y
            width: 24
            height: 24
            color: "#00ffffff"
            Image {
                width: 24
                height: 24
                source: "qrc:/other/icon/search.png"
            }
        }

        Rectangle {
            id: headerIcon
            x: parent.width-96
            y: 8
            width: 48
            height: 48
            color: "#ffffff"
            Image {
                width: 48
                height: 48
                source: "qrc:/other/icon/CustomerService.png"
            }
            MouseArea{
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton | Qt.RightButton;
                onClicked: {
                    //                                console.log("oks"+msgDataText.text)
                    if(mouse.button==Qt.RightButton){
                        console.log("teee")
                        userProfile_menu.popup()
                    }
                }
            }
            MyProFilePage{
                id:myProFilePage

            }

            Menu {
                id: userProfile_menu
                MenuItem{
                    text: "查看我的资料"
                    onClicked: {
//                        console.log(msgDataText.text)
//                        sendMsgData.text="["+char_msg_headerName.text+"]:"+msgDataText.text
                        console.log("dadasdsada")
                        myProFilePage.show()
                        myProFilePage.getMyProfileBySession("mySesionid ")

                    }
                }
            }
        }
    }

    //  内容页面
    SwipeView {
        id: swipeView
        anchors.fill: parent
        y:mainWindows_header.height
        width: parent.width
        height: parent.height-mainWindows_header.height
        //page 1
        Page{
            id:chatPage
            property ListView m_SessionlistView: m_SessionlistView
            Component {
                id: mySessionListDelegate
                ItemDelegate{
                    height: 60
                    width: 240
                    Rectangle{
                        x:5
                        border.width: 1
                        height: 60
                        color: "#ffffff"
                        Image {
                            id:imageIcon
                            width: 36
                            height: 36
                            y:parent.height/2-imageIcon.width/2
                            source: iconUser
                        }
                        Text {
                            id:chatList_name
                            y:parent.height/2-imageIcon.width/2
                            x:48
                            text: qsTr(name)
                            font.pointSize: 11
                        }
                        Text {
                            id:chatList_sessionid
                            y:parent.height/2-imageIcon.width/2
                            x:48+chatList_name.width
                            text: qsTr(sessionid)
                            font.pointSize: 6
                        }
                        Text {
                            x:180
                            y:parent.height/2-imageIcon.width/2
                            color: "#646464"
                            text: qsTr(time)
                            font.pointSize: 9
                        }

                        Text {
                            x:48
                            y:parent.height/2
                            color: "#646464"
                            text: qsTr(summaryNow)
                            font.italic: false
                            font.pointSize: 9
                        }
                    }
                    MouseArea {
                        anchors.fill: parent
                        acceptedButtons: Qt.LeftButton | Qt.RightButton;
                        onClicked: {
                            console.log(index,ListView.isCurrentItem)
                            if(chooesMessage){
                                console.log("chooseMessage")
                                sessionName=mySessionLists[chatPage.m_SessionlistView.currentIndex].name
                                sessionID=mySessionLists[chatPage.m_SessionlistView.currentIndex].sessionid
                                chatPage.m_SessionlistView.currentIndex = index  //实现item切换
                                // changUser(mySessionLists[chatPage.m_SessionlistView.currentIndex].name,mySessionLists[chatPage.m_SessionlistView.currentIndex].sessionid)
                                console.log(chatList_name.text,"=====",chatList_sessionid.text)
                                console.log("choose ",mySessionLists[chatPage.m_SessionlistView.currentIndex].sessionid)
                            }
                            if(mouse.button==Qt.RightButton){
                                console.log("menu")
                                if(chooesMessage){
                                    chat_menu.popup()
                                }else if(chooesAddress){
                                    chat_menu.popup()
                                }
                            }
                        }

                        onDoubleClicked: {
                            console.log("doubleClicked , doubleClicked , doubleClicked ！")
                            // 打开会话

                        }
                    }

                    Menu {
                        id: chat_menu
                        MenuItem{
                            text: qsTr("关闭会话")
                            onClicked: {
                                if(chooesMessage){
                                    console.log("message")
                                    sessionID="";
                                    sessionName=""
                                    listApi.deleteSession(sessionid,1)

                                }
                            }
                        }
                    }
                }

            }

            // 会话列表
            Rectangle {
                id:chat_New_Message_Box
                width: 240
                height: parent.height
                x: 0
                y: 0

                border.width: 1
                border.color: "#b4b4b4"
                Item {
                    width: 240; height: parent.height

                    ListView {
                        id:m_SessionlistView
                        highlight: Rectangle {
                            color: "#20000000"
                        }
                        anchors.fill: parent
                        model: mySessionLists
                        delegate: mySessionListDelegate
                        focus: true
                    }
                }
            }
            // 会话窗口
            Rectangle{
                x:240
                width: parent.width-chat_New_Message_Box.width
                height: parent.height
                color: "#666666"
                visible: haveSession
                // 会话窗口信息栏
                Rectangle{
                    height: 48
                    width: parent.width
                    color: "#99ffffff"
                    clip: true
                    Text {
                        x:6
                        id:chatWindwosName_Text
                        height: parent.height
                        text: qsTr(sessionName)
                        font.pointSize: 12
                        verticalAlignment: Text.AlignVCenter
                    }
                    Text {
                        x:6+chatWindwosName_Text.width
                        id:chatWindwosName_Sessionid
                        height: parent.height
                        text: qsTr("("+sessionID+")" )
                        font.pointSize: 7
                        verticalAlignment: Text.AlignVCenter
                    }
                    Rectangle{
                        x:chatWindwosName_Text.width+chatWindwosName_Sessionid.width+12
                        y:parent.height/2-width/2
                        width: 16
                        height: 16
                        radius: 8
                        color: getStatus()
                    }
                }
                // 会话窗口 显示列表窗
                Rectangle{
                    y:48
                    id:chat_windows
                    width: parent.width
                    height: parent.height-180
                    clip: true
                    Text {
                        //            id: name
                        text: qsTr("text"+chat_windows.width+"--"+parent.width)
                    }
                    Item {
                        id: chatListMessage_Team
                        //            y:48
                        width: parent.width
                        height: parent.height
                        Component {
                            id: char_Delegate
                            ItemDelegate{
                                width: parent.width
                                height: 30+msgBody_Box.height
                                Image {
                                    id:userChat_Icon_img
                                    x:mySend?chat_windows.width-width-5:5
                                    y:3
                                    width: 28
                                    height: 28
                                    source: userChat_Icon_img_src
                                }
                                Text {
                                    id:char_msg_headerName
                                    x:mySend?chat_windows.width-userChat_Icon_img.width-width-15:userChat_Icon_img.width+15
                                    //width: 98
                                    height: 20
                                    color: "#1f65f3"
                                    text: qsTr(chat_name)
                                    verticalAlignment: Text.AlignVCenter
                                }
                                Rectangle{
                                    id:msgBody_Box
                                    y:20
                                    x:mySend?10:36+userChat_Icon_img.x
                                    //height: 48
                                    width:chat_windows.width-userChat_Icon_img.width-25
                                    //width: msgDataText.width
                                    height:msgDataText.height<=30?30:msgDataText.height+5
                                    color: mySend?"#70000000":"#c8c9c9"
                                    radius: 6
                                    Text {
                                        x:5
                                        y: 4
                                        id: msgDataText
                                        width:msgDataText.text.length<=10?msgDataText.text.length*12+12:parent.width
                                        color: "#000000"
                                        wrapMode: Text.Wrap
                                        text: qsTr(msgBody)
                                        horizontalAlignment: mySend?Text.AlignRight:Text.AlignLeft
                                        rightPadding: 9
                                        leftPadding: 4
                                        font.pointSize: 12
                                    }

                                    MouseArea{
                                        anchors.fill: parent
                                        acceptedButtons: Qt.LeftButton | Qt.RightButton;
                                        onClicked: {
                                            //                                console.log("oks"+msgDataText.text)
                                            if(mouse.button==Qt.RightButton){
                                                console.log("teee")
                                                option_menu.popup()
                                            }
                                        }
                                    }
                                    Menu {
                                        id: option_menu
                                        MenuItem{
                                            text: "Edit"
                                            onClicked: {
                                                console.log(msgDataText.text)
                                                sendMsgData.text="["+char_msg_headerName.text+"]:"+msgDataText.text
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        ListView{
                            id:chat_listView
                            // height: msgDataText.height+64
                            width: 240
                            highlight: Rectangle {
                                color:"#20000000"
                            }
                            anchors.fill: parent
                            model: currSessionList
                            delegate: char_Delegate
                            focus: true

                        }

                        Rectangle{
                            x:parent.width-48
                            y:parent.height-48
                            radius: 8
                            width: 40
                            height: 24
                            color: "#3e8ddd"
                            Text {
                                id: unReadNum
                                color: "#ffffff"
                                width: parent.width
                                height: parent.height
                                text: qsTr(unread)
                                horizontalAlignment: Text.AlignHCenter
                                verticalAlignment: Text.AlignVCenter
                            }
                        }
                    }


                }
                // 输入框
                Rectangle{
                    y:parent.height-180+48
                    width: parent.width
                    height:180
                    color: "#aaffffff"
                    Rectangle{
                        //tool
                        width: 32
                        height: 180
                        color: "#30000000"
                        Image {
                            x:3
                            y:3
                            width: parent.width-6
                            height: width
                            source: "qrc:/other/icon/emoji.png"

                        }

                        Image {
                            x:3
                            y:width+10
                            width: parent.width-6
                            height: width
                            source: "qrc:/other/icon/File_Dir.png"
                            Rectangle{
                                width: parent.width
                                height: parent.height
                                color: "#00ffffff"
                                MouseArea{
                                    anchors.fill: parent
                                    onClicked: {
                                        // 发送动作
                                        //listApi.sendMessageByText("123",1,"dddadad")
                                        console.log("sdsafad")
                                        fileDialog.visible=true
                                        //                                doSendMessageByText(chatWindwosName_Sessionid.text,sendMsgData.text)
                                    }
                                }
                            }
                        }

                    }
                    TextEdit{
                        id:sendMsgData
                        x:38
                        y:8
                        width: parent.width-40
                        height: parent.height-16
                        text: "aaaa"
                        wrapMode: Text.Wrap
                    }


                    // send button
                    Rectangle{
                        x:parent.width-96
                        y:parent.height-96
                        width: 64
                        height: 32

                        radius: 8
                        color: "#ffffff"
                        border.width:1;
                        border.color: "darkgray";
                        Text {
                            text: qsTr("Send")
                            width: parent.width
                            height:parent.height
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                        }
                        MouseArea{
                            anchors.fill: parent
                            onClicked: {
                                // 发送动作
                                listApi.sendMessageByText("123",1,"dddadad")
                                //                                doSendMessageByText(chatWindwosName_Sessionid.text,sendMsgData.text)
                            }
                        }
                    }
                }

            }
        }

        //page 2
        Page{
            // friend list
            Component {
                id: myFriendListDelegate
                ItemDelegate{
                    height: 60
                    width: 240
                    Rectangle{
                        x:5
                        border.width: 1
                        height: 60
                        color: "#ffffff"
                        Image {
                            id:imageIcon
                            width: 36
                            height: 36
                            y:parent.height/2-imageIcon.width/2
                            source: iconUser
                        }
                        Text {
                            id:chatList_name
                            y:parent.height/2-imageIcon.width/2
                            x:48
                            text: qsTr(name)
                            font.pointSize: 11
                        }
                        Text {
                            id:chatList_sessionid
                            y:parent.height/2-imageIcon.width/2
                            x:48+chatList_name.width
                            text: qsTr(friendId)
                            font.pointSize: 6
                        }
                        Text {
                            x:180
                            y:parent.height/2-imageIcon.width/2
                            color: "#646464"
                            text: qsTr(time)
                            font.pointSize: 9
                        }

                        Text {
                            x:48
                            y:parent.height/2
                            color: "#646464"
                            text: qsTr(summary)
                            font.italic: false
                            font.pointSize: 9
                        }
                    }
                    MouseArea {
                        anchors.fill: parent
                        acceptedButtons: Qt.LeftButton | Qt.RightButton;
                        onClicked: {
                            console.log(index,ListView.isCurrentItem)
                            if(chooesAddress){
                                u_listView.currentIndex = index  //实现item切换
                                console.log("chooesAddress")
                            }
                            if(mouse.button==Qt.RightButton){
                                console.log("menu")
                                if(chooesMessage){
                                    chat_menu.popup()
                                }else if(chooesAddress){
                                    chat_menu.popup()
                                }
                            }
                        }

                        onDoubleClicked: {
                            console.log("doubleClicked , doubleClicked , doubleClicked ！")
                            // 创建会话
                            //                            listApi.createSession("123",1)
                        }
                    }

                    Menu {
                        id: chat_menu
                        MenuItem{
                            text:qsTr("开始会话")
                            onClicked: {
                                if(chooesAddress){
                                    // 创建会话
                                    doChooesMessage()
                                    listApi.createSession("123",1)
                                    chatPage.m_SessionlistView.currentIndex =0;

                                }
                            }
                        }
                        MenuItem{
                            text:qsTr("删除好友")
                            onClicked: {
                                if(chooesAddress){
                                    console.log("addr "+ friendId)
                                    listApi.deleteFriend(friendId)
                                    chatPage.m_SessionlistView.currentIndex =0;
                                }
                            }
                        }

                        MenuItem{
                            text:qsTr("查看好友信息")
                            onClicked: {
                                if(chooesAddress){
                                    console.log("addr "+ friendId)
                                }
                            }
                        }
                    }
                }

            }
            //Chat List
            Rectangle {
                width: 240
                height: parent.height
                x: 0
                y: 0
                border.width: 1
                border.color: "#b4b4b4"
                Item {
                    width: 200; height: parent.height
                    ListView {
                        id:u_listView
                        highlight: Rectangle {
                            color: "#20000000"
                        }
                        anchors.fill: parent
                        model: myFriendLists
                        delegate: myFriendListDelegate
                        focus: true
                    }
                }
            }

            Image {
                x: parent.width/2+120-64
                y: parent.height/2-64
                width: 128
                height: 128
                id: name
                source: "qrc:/other/icon/xiaoma.png"

            }

        }

        //page 3
        Page{
            id:application_Windows
            width: swipeView.width
            height: swipeView.height
            property int boxItemSize_width: 128
            property int boxItemSize_height: 64
            property int boxItemInterval: 5
            function boxItemGetX(i){
                console.log("in i= ",i)
                var lenSize = parseInt(application_Windows.width / (application_Windows.boxItemSize_width+application_Windows.boxItemInterval))
                console.log("lensize X= ",lenSize)
                var x=(parseInt(i%lenSize))*(application_Windows.boxItemSize_width+application_Windows.boxItemInterval)
                console.log("data X = ",x)
                return x
                //                console.log("dadadad",55)
                //                return 4
            }

            function boxItemGetY( i){
                console.log("in i= ",i)
                var lenSize = parseInt(application_Windows.width / (application_Windows.boxItemSize_width+application_Windows.boxItemInterval))
                console.log("lensize ",lenSize,"--",application_Windows.width ,"--",application_Windows.boxItemSize_height+application_Windows.boxItemInterval)
                var y =(parseInt(i/lenSize))*(application_Windows.boxItemSize_height+application_Windows.boxItemInterval)
                console.log("data Y = ",y)
                return parseInt(y)

            }
            Rectangle{
                x:application_Windows.boxItemGetX(1)
                y:application_Windows.boxItemGetY(1)
                width: application_Windows.boxItemSize_width
                height: application_Windows.boxItemSize_height
                color: "#999999"
                Text {

                    text: qsTr("text 123")
                }
            }
            Rectangle{
                Item {

                }
                x:application_Windows.boxItemGetX(0)
                y:application_Windows.boxItemGetY(0)
                width: application_Windows.boxItemSize_width
                height: application_Windows.boxItemSize_height
                color: "#999999"
                Text {

                    text: qsTr("text 123")
                }
            }
            Rectangle{
                x:application_Windows.boxItemGetX(2)
                y:application_Windows.boxItemGetY(2)
                width: application_Windows.boxItemSize_width
                height: application_Windows.boxItemSize_height
                color: "#999999"
                Text {

                    text: qsTr("text 123")
                }
            }

            Rectangle{
                x:application_Windows.boxItemGetX(3)
                y:application_Windows.boxItemGetY(3)
                width: application_Windows.boxItemSize_width
                height: application_Windows.boxItemSize_height
                color: "#999999"
                Text {

                    text: qsTr("text 123")
                }
            }
        }
        onCurrentIndexChanged: {
            if(currentIndex==0){
                doChooesMessage()
            }else if(currentIndex==1){
                doChooesAddress()
                //systemIcon.test()
            }else if(currentIndex==2){
                doChooesSetting()
            }else{
                doChooesMessage()
            }
        }

    }

    Connections{
        target: listApi
        onSessionSize:{
            console.log("session size : ",size)
            if(size>0){
                haveSession=true
            }else{
                haveSession=false
            }
        }

        onRecvMessageEven:{
            console.log("haveSession " ,name)
            systemIcon.showMessage("新消息:"+name+"("+sessionid+")",name+":"+summary+"["+time+"]")
        }
    }


}
